﻿using Common.Infrastructure.Service;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Common.Infrastructure
{
    public static class ServiceCallUtils
    {
        public static async Task<T> PostExecuteAsync<T>(IServiceProvider provider, string serviceCode, string url, object obj = null)
        {
            var service = GetServiceCallService(provider);
            return await service.PostExecuteAsync<T>(serviceCode, url, obj);
        }

        public static async Task<T> GetExecuteAsync<T>(IServiceProvider provider, string serviceCode, string url, Dictionary<string, object> param = null)
        {
            var service = GetServiceCallService(provider);
            return await service.GetExecuteAsync<T>(serviceCode, url, param);
        }

        public static async Task<T> GetGetAsync<T>(IServiceProvider provider, string serviceCode, string url, Dictionary<string, object> param = null)
        {
            var service = GetServiceCallService(provider);
            return await service.GetGetAsync<T>(serviceCode, url, param);
        }

        public static async Task<T> PostGetAsync<T>(IServiceProvider provider, string serviceCode, string url, object obj = null)
        {
            var service = GetServiceCallService(provider);
            return await service.PostGetAsync<T>(serviceCode, url, obj);
        }

        public static async Task<bool> GetDoAsync(IServiceProvider provider, string serviceCode, string url, Dictionary<string, object> param = null)
        {
            var service = GetServiceCallService(provider);
            return await service.GetDoAsync(serviceCode, url, param);
        }

        public static async Task<bool> PostDoAsync(IServiceProvider provider, string serviceCode, string url, object obj = null)
        {
            var service = GetServiceCallService(provider);
            return await service.PostDoAsync(serviceCode, url, obj);
        }

        public static async Task<List<T>> GetListAsync<T>(IServiceProvider provider, string serviceCode, string url, Dictionary<string, object> param = null)
        {
            var service = GetServiceCallService(provider);
            return await service.GetListAsync<T>(serviceCode, url, param);
        }

        public static async Task<List<T>> PostListAsync<T>(IServiceProvider provider, string serviceCode, string url, object obj)
        {
            var service = GetServiceCallService(provider);
            return await service.PostListAsync<T>(serviceCode, url, obj);
        }

        public static async Task<ApiPagedResult<T>> GetApiPageAsync<T>(IServiceProvider provider, string serviceCode, string url, Dictionary<string, object> param = null)
        {
            var service = GetServiceCallService(provider);
            return await service.GetApiPageAsync<T>(serviceCode, url, param);
        }

        public static async Task<ApiPagedResult<T>> PostApiPageAsync<T>(IServiceProvider provider, string serviceCode, string url, object obj)
        {
            var service = GetServiceCallService(provider);
            return await service.PostApiPageAsync<T>(serviceCode, url, obj);
        }

        private static IServiceCallService GetServiceCallService(IServiceProvider provider)
        {
            IServiceCallService capPublisher = null;
            try
            {
                if (provider != null)
                {
                    capPublisher = provider.GetService<IServiceCallService>();
                }
            }
            catch
            {
            }

            return capPublisher == null ? throw new CommonException("ServiceCallService null") : capPublisher;
        }
    }
}
